﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContexts;

public partial class Admin_User_ResetPwd : System.Web.UI.Page
{
    private UserDataContext userCntx = new UserDataContext();


    public int UserId
    {
        get
        {
            int id = 0;

            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                int.TryParse(Request.QueryString["id"], out id);

            return id;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ((BaseMasterPage)this.Master).CurrentPage = PageConfig.Pages.AdminUserResetPwd;
        if (!((BaseMasterPage)this.Master).AuthorizeCurrentUser())
            return;

        if (!IsPostBack)
        {
            trOldPwd.Visible = (UserAccess.CurrentUser.UserType == Convert.ToInt32(Enums.UserTypes.Admin));

            User user = userCntx.Users.SingleOrDefault(obj => obj.UserId == UserId);
            if (user == null)
                Response.Redirect("default.aspx");

            txtUsername.Text = user.Username;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        User user = userCntx.Users.SingleOrDefault(obj => obj.UserId == UserId);
        //add admin user validation
        if (trOldPwd.Visible)
        {
            if (user.Password != txtOldPwd.Text)
            {
                divError.InnerHtml = "Old password is invalid<br/><br/>";
                return;
            }
        }
        user.Password = txtPassword.Text;
        userCntx.SubmitChanges();

        Response.Redirect("default.aspx");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx");
    }
}
